<?php $emacs=0;

/*
 * Copyright 2008 Jeffrey Palm
 */

/* ==================================================
 * This is the main page. 
 * 
 * To use it copy the following php snippet into 
 * any of your pages -- probably index.php:
 *
 *   include 'content.php;
 *
 * If you have questions see Jeff mail.
 *
 * ==================================================
 */

include 'config.php';
include 'common.php';
include 'geo.php';

// Variables
define(PER_PAGE,10);
$start = $_REQUEST['start'];
if (!isset($start) || !$start || $start<-1) $start = 0;

// Maybe we have just asked to see one?
$theID = -1;
foreach ($_REQUEST as $k => $v) {
  if (preg_match('/^\d+$/',$k)) {
    $theID = $k;
    break;
  }
}

// Name of all the photos (\d+)
$names = array();

// maybe add it
$des = $theID . '.des';
$loc = $theID . '.loc';
$added = FALSE;
if (is_file($des) && is_file($loc)) {
  $names[] = $theID;
  $added = TRUE;
 }

if ($theID == -1 || !$added) {
  // find all the photos that are just numbers
  $newNames = readNames('.');
  foreach ($newNames as $nn) $names[]= $nn;
 }

if (isset($_REQUEST['all'])) {
  $stop = -1;
 } else {
  $stop = $start + PER_PAGE;
 }

if ($stop>count($names)) {
  $stop = count($names);
 }

if (count($names) == 0) {
  note('Take some pictures!');
 }

rsort($names);


echo '<a name="top"></a>';
$title = config('title',FALSE);
echo '<div class="title">';
if ($title) {
  echo $title . ' ';
 }

echo '( ';
if ($stop == -1) {
  echo 'all';
 } else if ($theID != -1) {
  echo $theID;
 } else {
  echo ($start+1) . ' - ' . $stop;
 }
echo ' )</div>';

echo '<span style="vertical-align:top">';
function pagination() {
  global $start;
  global $names;
  global $stop;
  global $theID;
  global $CONFIG;

  echo '<p>';

  // back
  if ($stop != -1) {
    if ($start>0) {
      $newStart = $start-PER_PAGE;
      if ($newStart<0) $newStart = 0;
      echo '<a href="' . $_SERVER['PHP_SELF'] . '?start=' . $newStart . '#top"> back </a>';
    } else {
      echo ' back ';
    }
		
    // more
    echo ' | ';
    if ($stop < count($names)) {
      $newStart = $stop;
      echo '<a href="' . $_SERVER['PHP_SELF'] . '?start=' . $newStart . '#top"> more </a>';
    } else {	
      echo ' more ';
    }
		
    // all
    echo ' | ';
    echo '<a href="' . $_SERVER['PHP_SELF'] . '?all#top"> all </a>';
  } else {
    echo '<a href="' . $_SERVER['PHP_SELF'] . '#top"> latest </a>';
  }
  if ($theID != -1) {
    echo ' | ';
    echo '<a href="' . $_SERVER['PHP_SELF'] . '#top"> latest </a>';
  }

  echo '</p>';

}

?>

<p>
<table border=0 cellpadding=0 cellspacing=0 width="100%">
  <tr><td align="left"><p>
  <?php
  pagination();
?>
</p>
</td><td align="right">

  </td></tr></table>
  </p>
  </td></tr></table>

  <p>
  <table border=0 cellpadding=0 cellspacing=0>
  <tr><td>
  <?php

  echo '<span class="content" style="margin:15px; width:350px;">';
echo '<p align="center">';

// left side
echo '<div class="content" style="text-align: center; ">';
echo '<p align="center">';
echo '<table border="0" cellspacing="0" cellpadding="0">';

for ($i=$start; ($stop == -1 || $i<$stop) && $i<count($names); $i++) {

	$name = $names[$i];

	$des = removeWhitespace(readFileAll($name . '.des'));
	$loc = removeWhitespace(readFileAll($name . '.loc'));
	$who = removeWhitespace(readFileAll($name . '.who'));

	$date = format_date($name);

	$img = $name . ($theID == -1 ? '' : '-full') . '.jpg';
	$cls = $theID == -1 ? 'smallImage' : 'largeImage';
	
	// A hack for now, since I don't have all the large images
	$ncls = $theID == -1 ? 'largeImage' : 'smallImage';
	$nimg = $name . ($theID == -1 ? '-full' : '') . '.jpg';

	echo '<tr>';
	echo '<td align="left">';
	echo '<a name="'.$name.'"></a>';
	$showLink = $theID == -1 && is_file($nimg) && is_file($img);
	if ($showLink) echo '<a href="?'.$name.'#top">';
	echo '<img class="' . $cls . '" src="' . $img . '" />';
	if ($showLink) echo '</a>';
	echo '</p></td>';
	if ($theID != -1) {
	  echo '</tr><tr>';
	} else {
	  echo '<td>&nbsp;&nbsp;&nbsp;</td>';
	}
	echo '<td align="left" valign="top">';
	echo '<span class="photo">';
	if ($des) {
	  echo '<span class="description">';
	  if ($showLink) echo '<a href="?'.$name.'#top">';
	  echo $des;
	  if ($showLink) echo '</a>';
	  echo '</span>';
	  echo '<br/>';
	}
	echo '<span class="location">' . $loc . '</span>';
	echo '<br/>';
	echo '<span class="date">' . $date . '</span>';
  if ($CONFIG['show_who']) {
    echo '<br/>';
    echo '<span class="who">by ' . $who . '</span>';
  }
  if (hasGeoInformation($name)) {
	echo '<br/>';
    echo '[';
    echo '<a id="a_' . $name . '" href="#' . $name . '" onclick="openMapFrame(\'' . $name . '\')" >map</a>';
    echo ']';
    echo '<div style="display:none" id="frame_' . $name 
      .  '"><iframe style="height:500px" id="iframe_' . $name . '"></iframe></div>';
  }
  echo '</span>';
	echo '</td>';
	echo '</tr>';
  echo "\n";

 }

echo '</table>';
echo '</p>';
echo '</div>';

echo '</p>';
echo '</span>';

?>

</td><td valign=top>

  </td></tr>
  </table>
  </p>

<?php
  echo '<p>';
  pagination();
  echo '</p>';
?>